package com.cwj.mylibrary;

import android.annotation.SuppressLint;
import android.content.Context;
import android.os.Build;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.cwj.mylibrary.R;

/**
 * Toast弹窗工具类
 */
public class ToastUtil {

    private static Toast mToast;
    @SuppressLint("StaticFieldLeak")
    private static TextView mTvMessage;
    @SuppressLint("StaticFieldLeak")
    private static ImageView iv;

    /**
     * miui部分版本会自带包名 用此方法解决该Bug
     */
    private static void createToast(Context context, CharSequence message, int img, int duration) {
        mToast = new Toast(context);

        LayoutInflater inflate = (LayoutInflater)
                context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        @SuppressLint("InflateParams") View v = inflate.inflate(R.layout.transient_notification, null);
        mTvMessage = v.findViewById(R.id.message);
        iv = v.findViewById(R.id.iv);
        mTvMessage.setText(message);
        iv.setImageResource(img);

        if (img > 0) {
            iv.setVisibility(View.VISIBLE);
        } else {
            iv.setVisibility(View.GONE);
        }

        mToast.setView(v);
        mToast.setDuration(duration);
    }

    private static void show(Context context, CharSequence message, int img, int duration) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {//8.0对Toast做了改动
            createToast(context, message, img, duration);
        } else {
            if (mToast == null) {
                createToast(context, message, img, duration);
            } else if (mTvMessage != null) {
                mTvMessage.setText(message);
            } else {
                mToast = null;
                show(context, message, img, duration);
            }
        }

        mToast.setGravity(Gravity.CENTER, 0, 0);
        mToast.show();
    }

    private static void showImg(Context context, CharSequence message, int img, int duration) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {//8.0对Toast做了改动
            createToast(context, message, img, duration);
        } else {
            if (mToast == null) {
                createToast(context, message, img, duration);
            } else if (mTvMessage != null) {
                mTvMessage.setText(message);
            } else {
                mToast = null;
                show(context, message, img, duration);
            }
        }

        mToast.setGravity(Gravity.CENTER, 0, 0);
        mToast.show();
    }

    /**
     * short Toast
     */
    public static void s(Context context, String msg) {
        show(context, msg, 0, Toast.LENGTH_SHORT);
    }

    /**
     * long Toast
     */
    public static void l(Context context, String msg) {
        show(context, msg, 0, Toast.LENGTH_LONG);
    }

    /**
     * Image short Toast
     */
    public static void imageLS(Context context, String msg, int img) {
        showImg(context, msg, img, Toast.LENGTH_SHORT);
    }

    /**
     * Image long Toast
     */
    public static void imageLL(Context context, String msg, int img) {
        showImg(context, msg, img, Toast.LENGTH_LONG);
    }
}
